package com.snape.service.impl;

import com.snape.dao.UserDao;
import com.snape.entity.User;
import com.snape.entity.User_Room;
import com.snape.service.UserService;
import com.snape.utils.LoginUtils;
import com.snape.utils.StatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liuqingcan_snape on 2017/3/10.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    //模拟登录
    public Map<String,Object> doLogin(String username, String userpwd){
        Map<String,Object> result = new HashMap<String, Object>();
        Map<String,String> loginResult = new HashMap<String, String>();
        if (username == null||userpwd == null) {
            result.put("statusCode", StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data", null);
            result.put("statusMsg", StatusUtils.INVALID_PARAMETERS.getStatusName());
        }

        try {
            loginResult = LoginUtils.LoginHelp(username,userpwd);
            //如果登录失败,直接返回
            String status = loginResult.get("status");
            if (!status.equals("y")){
                result.put("statusCode",StatusUtils.LOGIN_FAILED.getStatusCode());
                result.put("data",loginResult);
                result.put("statusMsg",StatusUtils.LOGIN_FAILED.getStatusName());
            } else {
                String cookies = loginResult.get("cookie");
                //存数据库,因为是别人的平台,我获取不到
                StatusUtils utils = this.queryByUserName(username);
                int flag = 0;
                User user = new User();
                switch (utils.getStatusCode()) {
                    case 1003://查出来
                        try {
                            user = userDao.queryByUserName(username);
                            userDao.updateUserCookie(cookies,user.getId());
                            flag = 1;
                        } catch (Exception e) {
                        }
                        break;
                }
                 if (flag == 0){
                     user.setPassword(userpwd);
                     user.setName(username);
                     user.setCookie(cookies);
                     userDao.insertUser(user);
                     user = userDao.queryByUserName(username);
                 }

                result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
                result.put("data",user);
                result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
            }


        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SERVICE_ABNORMAL.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SERVICE_ABNORMAL.getStatusName());
        }

        return result;
    }


    /**
     * 查询是否存在当前user
     * @param username
     * @return  -2:查询异常
     *          -1: 参数不合法
     *          0: 存在
     *          1: 不存在
     *
     */
    private StatusUtils  queryByUserName (String username){
        User user  = null;
//        int result = -1;

        if (username==null){
            return StatusUtils.INVALID_PARAMETERS;
        }

        try {
            user = userDao.queryByUserName(username);
            if (user != null){
                return StatusUtils.USER_EXIST_ERROR;
            }else {
                return StatusUtils.SUCCESS;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
       return StatusUtils.FAIL;
    }

    /**
     * 查询 user
     * @param id
     * @return
     */
    public Map<String,Object>  queryById (int id){
        Map<String,Object> result = new HashMap<String, Object>();
        if (id<=0){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }
        User user = null;

        try {
            user = userDao.queryById(id);
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",user);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        }catch (Exception e){
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SERVICE_ABNORMAL.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SERVICE_ABNORMAL.getStatusName());
        }

        return result;
    }

    /**
     * 注册 user
     * @param user
     * @return
     * 先要查询是否存在该user
     */
    public Map<String,Object> insertUser(User user){
        Map<String,Object> result = new HashMap<String, Object>();
        if (user==null||user.getName()==null){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            StatusUtils utils = this.queryByUserName(user.getName());
            int flag = 0;
            switch (utils.getStatusCode()){
                case 1003:
                    result.put("statusCode",StatusUtils.USER_EXIST_ERROR.getStatusCode());
                    result.put("statusMsg",StatusUtils.USER_EXIST_ERROR.getStatusName());
                    break;
                case 3002:
                    result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
                    result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
                    break;
                case 1:
                    flag = 1;
                    break;
            }
            if (flag==1){
                userDao.insertUser(user);
                result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
                result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
            }
            result.put("data",null);


        } catch (Exception e) {
//            e.printStackTrace();
            result.put("statusCode",StatusUtils.INSERT_USER_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INSERT_USER_ERROR.getStatusName());
        }
        return result;
    }

    /**
     * 更新 user
     * @param user
     * @return
     */
    public Map<String,Object>  updateUser(User user){
        Map<String,Object> result = new HashMap<String, Object>();
        if (user==null){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            userDao.updateUser(user);
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
//            e.printStackTrace();
            result.put("statusCode",StatusUtils.UPDATE_USER_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.UPDATE_USER_ERROR.getStatusName());

        }
        return result;
    }

    /**
     * 查询 cookie
     * @param userId
     * @return
     */
    public Map<String,Object> queryCookie(int userId) {
        Map<String,Object> results = new HashMap<String, Object>(64);
        if (userId < 0){
            results.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            results.put("data",null);
            results.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return results;
        }

        try {
            String cookie = userDao.selectUserCookie(userId);
            if (cookie == null){
                results.put("statusCode",StatusUtils.WRONG_COOKIE.getStatusCode());
                results.put("data",cookie);
                results.put("statusMsg",StatusUtils.WRONG_COOKIE.getStatusName());
            }else {
                results.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
                results.put("data",cookie);
                results.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
            }
        } catch (Exception e) {
            e.printStackTrace();
            results.put("statusCode",StatusUtils.SERVICE_ABNORMAL.getStatusCode());
            results.put("data",null);
            results.put("statusMsg",StatusUtils.SERVICE_ABNORMAL.getStatusName());
        }

        return  results;
    }




    /*
     * 有关user_room表的api
     */

    /**
     * 插入直播地址
     * @param userId
     * @param roomId
     * @param address
     * @return
     */
    public Map<String,Object> insertLiveAddress(int userId,int roomId,String address){
        Map<String,Object> result = new HashMap<String, Object>();
        if (userId<=0 || roomId<=0 || address==null){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            ArrayList<User_Room> user_room =  userDao.queryStatusOfUserToVideo(userId,roomId);
            if (user_room.size() == 0){
                User_Room ur = new User_Room();
                ur.setUser_id(userId);
                ur.setRoom_id(roomId);
                ur.setVideo_url(address);
                userDao.insertUserToVideo(ur);
            }else {
                userDao.insertLiveAddress(userId,roomId,address);
            }

//
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.INSERT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INSERT_DATA_ERROR.getStatusName());
        }

        return result;
    }

    /**
     * 更新收藏状态
     * @param userId
     * @param roomId
     * @return
     */
    public Map<String,Object> updateCollectionStatus(int userId,int roomId){
        Map<String,Object> result = new HashMap<String, Object>();
        if (userId<=0 || roomId<=0 ){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            ArrayList<User_Room> user_room =  userDao.queryStatusOfUserToVideo(userId,roomId);
            if (user_room.size() == 0){
                User_Room ur = new User_Room();
                ur.setUser_id(userId);
                ur.setRoom_id(roomId);
                userDao.insertUserToVideo(ur);
            }

            userDao.updateCollectionStatus(userId,roomId);
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.UPDATE_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.UPDATE_DATA_ERROR.getStatusName());
        }

        return  result;
    }

    /**
     * 查询user_room所有的字段
     * @param userId
     * @param roomId
     * @return
     */
    public Map<String,Object> queryAllStatusOfUserToVideo(int userId,int roomId){
        Map<String,Object> result = new HashMap<String, Object>();
        if (userId<=0 || roomId<=0 ){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
//            ArrayList<User_Room> user_room =  userDao.queryStatusOfUserToVideo(userId,roomId);
//            if (user_room.size() == 0){
//                User_Room ur = new User_Room();
//                ur.setUser_id(userId);
//                ur.setRoom_id(roomId);
//                userDao.insertUserToVideo(ur);
//            }
            ArrayList<User_Room> user_room  =  userDao.queryStatusOfUser(userId);
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",user_room);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return  result;
    }

    /**
     * 更新观看记录
     * @param userId
     * @param roomId
     * @return
     */
    public Map<String,Object> updateRecordsStatus(int userId,int roomId){
        Map<String,Object> result = new HashMap<String, Object>();
        if (userId<=0 || roomId<=0 ){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {
            ArrayList<User_Room>  user_room =  userDao.queryStatusOfUserToVideo(userId,roomId);
            if (user_room.size() == 0){
                User_Room ur = new User_Room();
                ur.setUser_id(userId);
                ur.setRoom_id(roomId);
                userDao.insertUserToVideo(ur);
            }


            userDao.updateRecordsStatus(userId,roomId);
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.UPDATE_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.UPDATE_DATA_ERROR.getStatusName());
        }

        return  result;
    }

    /**
     * 更新是否录播记录
     * @param userId
     * @param roomId
     * @return
     */
    public Map<String,Object> updateWebcastingStatus(int userId,int roomId){
        Map<String,Object> result = new HashMap<String, Object>();
        if (userId<=0 || roomId<=0 ){
            result.put("statusCode",StatusUtils.INVALID_PARAMETERS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.INVALID_PARAMETERS.getStatusName());
            return result;
        }

        try {

            ArrayList<User_Room> user_room =  userDao.queryStatusOfUserToVideo(userId,roomId);
            if (user_room.size() == 0){
                User_Room ur = new User_Room();
                ur.setUser_id(userId);
                ur.setRoom_id(roomId);
                userDao.insertUserToVideo(ur);
            }

            userDao.updateWebcastingStatus(userId,roomId);
            result.put("statusCode",StatusUtils.SUCCESS.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode",StatusUtils.UPDATE_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.UPDATE_DATA_ERROR.getStatusName());
        }

        return  result;
    }






}



